package com.daidai.controller;

import com.daidai.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        System.out.println("user save...");
        return "user save success...";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String addUser(@RequestParam("username")String username, @RequestParam("password")String password){
        System.out.println("add..."+username+":"+password);
        return "success add.."+username+":"+password;
    }

    //测试pojo类型参数传递
    @RequestMapping("/pojo")
    @ResponseBody
    public User addUser(User user){
        System.out.println(user);
        return user;
    }

    @RequestMapping("/likes")
    @ResponseBody
    public String likes(String[] likes){
        for (String like : likes) {
            System.out.println(like);
        }
        return "likes";
    }

    @RequestMapping("/language")
    @ResponseBody
    public String language(@RequestParam List<String> languages){

        languages.forEach(l -> {
            System.out.println(l);
        });
        System.out.println(languages);

        return "{'info':'success'}";
    }

    @RequestMapping("/userJsonParam")
    @ResponseBody
    public String userInfo(@RequestBody User user){
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/dade")
    @ResponseBody
    public String getDate(@DateTimeFormat(pattern="yyyy-mm-dd hh-MM-ss") Date date){
        System.out.println(date);
        return ""+date.getTime();
    }

    //响应json数据
    @RequestMapping("/responseJson")
    @ResponseBody
    public User responseJson(User user){
        System.out.println(user);
        return user;
    }


}
